﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TLL.Log
{
    public class LogItem
    {
        private int id = 0;
        private string text = string.Empty;
        private bool isPriority = false;
        private DateTime create = DateTime.MinValue;
        private LogStatuses status = default(LogStatuses);
        private DateTime? complite = null;
        private DateTime deadLine = DateTime.MinValue;
        private LogProject project = null;
        private List<LogDate> log = null;
        private List<LogChange> logChanges = null;
        
        public int ID { get { return this.id; } }
        public string Text { get { return this.text; } }
        public bool IsPriority { get { return this.isPriority; } }
        public DateTime Create { get { return this.create; } }
        public LogStatuses Status { get { return this.status; } }
        public DateTime? Complite { get { return this.complite; } }
        public DateTime? DeadLine { get { return this.deadLine; } }
        public LogProject Project { get { return this.project; } }
        public List<LogDate> Log { get { return this.log; } }
        public List<LogChange> LogChanges { get { return this.logChanges; } }

        public bool HasLog { get { return this.log != null; } }
        public bool HasLogChange { get { return this.logChanges != null; } }
        public static LogItem Empty { get { return new LogItem(0, string.Empty, false, DateTime.MinValue, null, DateTime.MinValue, null, default(LogStatuses), null, null); } }


        public LogItem(int id, string text, bool isPriority, DateTime create, DateTime? Complite, DateTime deadLine, LogProject project, LogStatuses status , List<LogDate> log, List<LogChange> logchages)
        {
            this.id = id;
            this.text = text;
            this.isPriority = isPriority;
            this.create = create;
            this.deadLine = deadLine;
            this.project = project;
            this.status = status;
            this.log = log;
            this.logChanges = logchages;
            this.complite = Complite;
        }
        public LogItem(string text, bool isPriority, DateTime create, DateTime? Complite, DateTime deadLine, LogProject project, LogStatuses status, List<LogDate> log, List<LogChange> logchages)
        {
            this.text = text;
            this.isPriority = isPriority;
            this.create = create;
            this.deadLine = deadLine;
            this.project = project;
            this.status = status;
            this.log = log;
            this.logChanges = logchages;
            this.complite = Complite;
        }
        public LogItem() { }

        /// <summary>
        /// Add to log. If ID == 0, method set the last ID in object
        /// </summary>
        public void AddLog(LogDate item)
        {
            if (this.log == null)
                this.log = new List<LogDate>();

            if (item.ID == 0)
                this.log.Add(new LogDate(
                    this.GetLastIDLogs(this),
                    item.Date.Value,
                    item.Time
                ));
            else
                this.log.Add(item);
        }
        /// <summary>
        /// Add to changeLog. If ID == 0, method set the last ID in object
        /// </summary>
        public void AddChangeLog(LogChange item)
        {
            if (this.logChanges == null)
                this.logChanges = new List<LogChange>();

            if (item.ID == 0)
                this.logChanges.Add(new LogChange(
                    this.GetLastIDLogs(this),
                    item.Date.Value,
                    item.Target,
                    item.Value
                ));
            else
                this.logChanges.Add(item);
        }

        public void ChangeStatus(LogStatuses status)
        {
            if (status == LogStatuses.Complite)
            { 
                this.status = LogStatuses.Complite;
                this.complite = DateTime.Now;
            }
            else
            {
                this.status = status;
                this.complite = null;
            }
        }
        public void Change(string text, LogStatuses status, DateTime create, DateTime? complite, DateTime deadLine, List<LogDate> log)
        {
            this.text = text;
            this.status = status;
            this.create = create;
            this.deadLine = deadLine;
            this.log = log;
            this.complite = complite;
        }
        public LogItem Dublicate()
        {
            return new LogItem(this.id, this.text, this.isPriority, this.create, this.complite, this.deadLine, this.project, this.status, this.log, this.logChanges);
        }
        public LogTime GetTime()
        {
            var res = LogTime.Empty;
            if (this.Log != null)
                foreach (var datelog in Log)
                    res.Add(datelog.Time);

            return res;
        }
        private int GetLastIDLogs(LogItem item)
        {
            var res = 0;
            if (item.Log != null)
                foreach (var logItem in item.Log)
                    if (res < logItem.ID)
                        res = logItem.ID;

            if (item.LogChanges != null)
                foreach (var logItem in item.LogChanges)
                    if (res < logItem.ID)
                        res = logItem.ID;

            return res + 1;
        }
    }
}
